package com.vipsoft.locker.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vipsoft.base.exception.CustomException;
import com.vipsoft.base.entity.locker.CategoryInfo;
import com.vipsoft.locker.mapper.CategoryMapper;
import com.vipsoft.locker.service.ICategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;


@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, CategoryInfo> implements ICategoryService {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 创建记录
     *
     * @param param
     * @return
     */
    @Override
    public boolean save(CategoryInfo param) {
        boolean result = false;
        //重复检查
        LambdaQueryWrapper<CategoryInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.and(j -> j.eq(CategoryInfo::getMemberId, param.getMemberId()));
        queryWrapper.and(j -> j.eq(CategoryInfo::getCategoryName, param.getCategoryName()));
        CategoryInfo entity = this.getOne(queryWrapper,false);
        if (StrUtil.isEmpty(param.getId())) {
            if (entity != null) {
                throw new CustomException(6002, "不能重复添加数据 ");
            }
            result = super.save(param);
        } else {
            if (entity != null && !entity.getId().equals(param.getId())) {
                throw new CustomException(6001, "分类名称，存在重复");
            }
            result = super.updateById(param);
        }
        return result;
    }
}
